
<div class=" layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <a lay-href="">首页</a>
        <a>商户管理</a>
        <a><cite>支付渠道</cite></a>
    </div>
</div>

<style>
    .layui-form-label {
        width:120px !important;
        margin-left:1%;
    }
    .layui-input-inline {
        width: 80% !important;
    }
</style>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-tab layui-tab-brief">
            <ul class="layui-tab-title">
                <li class="layui-this">渠道配置</li>
            </ul>
            <div class="layui-tab-content">
                <form class="layui-form center">
                    <input type="hidden" id="mchIds" name="mchIds">
                    <input type="hidden" id="productId" name="productId">
                    <div class="layui-form-item">
                        <label class="layui-form-label">产品名称</label>
                        <div class="layui-input-inline">
                            <input type="text" name="productName" id="productName" style="border-width: 0px;" required lay-verify="required" autocomplete="off" class="layui-input" readonly="readonly" disabled="disabled">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">产品类型</label>
                        <div class="layui-input-inline" >
                            <input type="radio" name="productType" title="收款" value="1" disabled />
                            <input type="radio" name="productType" title="充值" value="2" disabled />
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">商户费率(%)</label>
                        <div class="layui-input-inline">
                            <input type="text" id="mchRate" name="mchRate"  placeholder="请输入商户费率" autocomplete="off" class="layui-input">
                            <span style="color: red">填入费率后会将所有商户设置为统一费率， 如不更新商户原费率请置空！</span>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">接口模式</label>
                        <div class="layui-input-inline" >
                            <input type="radio" name="ifMode" title="单独" value="1" lay-filter="ifModeFilter" checked="checked"/>
                            <input type="radio" name="ifMode" title="轮询" value="2" lay-filter="ifModeFilter"/>
                        </div>
                    </div>

                    <div class="layui-form-item" id="payPassageIdDiv">
                        <div class="layui-form-item">
                            <label class="layui-form-label">支付通道</label>
                            <div class="layui-input-inline" >
                                <select name="payPassageId" id="payPassageId" lay-filter="payPassageFilter">
                                    <option value="">选择支付通道</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">通道子账户</label>
                            <div class="layui-input-inline" >
                                <select name="payPassageAccountId" id="payPassageAccountId" lay-filter="payPassageAccountFilter">
                                    <option value="">选择通道子账户</option>
                                </select>
                            </div>
                        </div>
                    </div>

                    <div class="layui-form-item" id="pollWeightDiv" style="display: none">
                        <label class="layui-form-label">支付通道</label>
                        <div class="layui-input-inline" >
                            <table id="XxPay_Mgr_Poll_dataAll" lay-filter="XxPay_Mgr_Poll_dataAll"></table>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">状态</label>
                        <div class="layui-input-inline" >
                            <input type="radio" name="status" title="开启" value="1" checked/>
                            <input type="radio" name="status" title="关闭" value="0" />
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-input-inline">
                            <button type="button" class="layui-btn" lay-submit="" lay-filter="update">保存</button>
                            <button type="button" id="btnBack" class="layui-btn">返回</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<script>

    layui.use(['form','table','util','admin',],function(){
        var form = layui.form
                , $ = layui.$
                , admin = layui.admin
                , layer = layui.layer
                , element = layui.element
                , table = layui.table;

        element.render('breadcrumb', 'breadcrumb'); //渲染导航信息

        var router = layui.router();
        var mchIds = router.search.mchIds;
        var productId = router.search.productId;

        $("#mchIds").val(mchIds);
        $("#productId").val(productId);

        // 获取产品详情
        admin.req({
            type: 'post',
            url: layui.setter.baseUrl + '/config/pay_product/get',
            data: {id: productId},
            error: function(err){
                layer.alert(err);
            },
            success: function(res){
                if(res.code == 0){
                    $('#productName').val(res.data.productName);
                    $("input[name='productType'][value='"+res.data.productType+"']").attr("checked",true);
                    form.render();
                }
            }
        });

        // 加载支付通道列表
        admin.req({
            type: 'post',
            url: layui.setter.baseUrl + '/config/common/pay_passage_product',
            data: {productId: productId},
            error: function(err){
                layer.alert(err);
            },
            success: function(res){
                if(res.code == 0){
                    var payPassageList = res.data;
                    //遍历赋值
                    for(var i in payPassageList){
                        $("#payPassageId").append('<option value= '+payPassageList[i].id+'>'+payPassageList[i].passageName+' [' + payPassageList[i].passageRate + '%]' +'</option>');
                    }
                    form.render('select');
                }
            }
        });

        // 监控通道列表下拉选择事件
        form.on('select(payPassageFilter)', function(data){
            var payPassageId = data.value;
            // 清除旧的
            $("#payPassageAccountId option").each(function(){  //遍历所有option
                var val = $(this).val();   //获取option值
                if(val != '') {
                    this.remove();
                }
            });
            admin.req({
                type: 'post',
                url: layui.setter.baseUrl + '/config/common/pay_passage_account',
                data: {payPassageId: payPassageId},
                error: function(err){
                    layer.alert(err);
                },
                success: function(res){
                    if(res.code == 0){
                        var payPassageAccountList = res.data;
                        //遍历赋值
                        for(var i in payPassageAccountList){
                            $("#payPassageAccountId").append('<option value= '+payPassageAccountList[i].id+'>'+payPassageAccountList[i].accountName+' [' + payPassageAccountList[i].id + ']' +'</option>');
                        }
                        form.render('select');
                    }
                }
            });
        });

        // 通道轮询
        table.render({
            elem: '#XxPay_Mgr_Poll_dataAll'
            ,url: layui.setter.baseUrl + '/mch_pay_passage/batch_poll_get' //
            ,where: {
                access_token: layui.data(layui.setter.tableName).access_token,
                productId: productId
            }
            ,id: 'tableReload'
            ,page: false
            ,cols: [[
                {field: 'ok', type: 'checkbox', fixed: 'left'}
                ,{field: 'id', title: '通道ID', width: 180}
                ,{field: 'passageName', title: '通道名称', width: 250}
                ,{field: 'weight', edit: 'text', title: '轮询权重(1-9)', width: 240}
            ]]
            ,skin: 'line'
            ,done: function(res, curr, count){}
        });

        form.on('submit(update)', function(data){
            layer.confirm('该操作会覆盖商户之前配置，确认修改吗？', function(index){
                layer.close(index);
                var mchRate = $.trim($('#mchRate').val());
                if(mchRate && isNaN(mchRate)){ //设置了 && 不是数字格式
                    layer.alert("费率设置值有误！",{title: '提示'});
                    return false;
                }

                var payPassageId = $.trim($('#payPassageId').val());
                var ifMode = $('input[name="ifMode"]:checked ').val();
                if(ifMode == 1 && payPassageId == '') {
                    layer.alert("请选择支付通道",{title: '提示'});
                    return false;
                }

                // 获取表格选中行的数据
                var checkDatas = table.checkStatus('tableReload');
                if(ifMode == 2 && checkDatas.data.length <= 0) {
                    layer.alert("请选择支付通道",{title: '提示'});
                    return false;
                }
                //console.log(checkStatus.checkDatas);
                var poll = [];
                $.each(checkDatas.data, function(index, val) {
                    console.log(val.id + "," + val.weight);

                    var p = {};
                    p.payPassageId = val.id;
                    p.weight = val.weight;
                    poll.push(p);

                } );
                // 转成轮询参数json
                var pollParam = JSON.stringify(poll);
                data.field.pollParam = pollParam;

                admin.req({
                    type: 'post',
                    url: layui.setter.baseUrl + '/mch_pay_passage/update_batch',
                    data: data.field,
                    error: function(err){layer.alert(err.msg,{title:"请求失败"})},
                    success: function(res){
                        if(res.code == 0){
                            layer.alert(res.msg, {title: '保存成功'},function(index){
                                layer.close(index);
                                location.hash = '/merchant/pay_passage/batch_list/mchIds=' + mchIds;
                            });
                        }
                    }
                });
                return false;
            });
        });

        $('#btnBack').on('click', function() {
            location.hash = '/merchant/pay_passage/batch_list/mchIds=' + mchIds;
        });

        form.on('radio(ifModeFilter)', function(data){
            if(data.value == 1) {
                $("#payPassageIdDiv").show();
                $("#pollWeightDiv").hide();
            }else if(data.value == 2) {
                $("#payPassageIdDiv").hide();
                $("#pollWeightDiv").show();
            }
        });
        form.render();
    });

</script>
